package com.gitee.wsl.compose.chart.xy.bar.shape

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.*
import com.gitee.wsl.common.ui.ext.color.rgb
import com.gitee.wsl.unit.angle.AngleUnit
import com.gitee.wsl.unit.angle.deg
import com.gitee.wsl.unit.angle.ext.cos
import com.gitee.wsl.unit.angle.ext.sin
import kotlin.math.roundToInt

class Bar3D {
    //3D效果厚度
    private var mThickness = 20

    //偏移角色度
    private var mAngle = 45.deg

    //透明度
    private var mAlpha = 200

    //3D效果厚度
    private var mAxisBaseThickness = 20

    //底盘颜色
    private var mAxisBaseColor: Color = Color.rgb(73, 172, 72)

    private val mPathRectangle2D: Path = Path()

    private val mPaintLine: Paint = Paint()

    //3D浅色画笔
    private var mPaint3D: Paint= Paint()
    private var mPaintBase: Paint= Paint()
    private var mPaintBase3D: Paint= Paint()

    /**
     * 得到水平偏移量
     * @param thickness    厚度
     * @param angle        角度
     * @return    水平偏移量
     */
    fun getOffsetX(thickness: Double, angle: AngleUnit): Double {
        return thickness * angle.cos()
    }


    /**
     * 得到垂直偏移量
     * @param thickness    厚度
     * @param angle        角度
     * @return    垂直偏移量
     */
    fun getOffsetY(thickness: Double, angle: AngleUnit): Double {
        return thickness * angle.sin()
    }


    /**
     * 返回水平偏移量
     * @return 偏移量
     */
    fun getOffsetX(): Double {
        return getOffsetX(mAxisBaseThickness.toDouble(), mAngle)
    }


    /**
     * 返回垂直偏移量
     * @return 偏移量
     */
    fun getOffsetY(): Double {
        return getOffsetY(mAxisBaseThickness.toDouble(), mAngle)
    }

    /**
     * 计算同标签多柱形时的Y分隔
     * @param YSteps    Y轴步长
     * @param barNumber  柱形个数
     * @return 返回单个柱形的高度及间距
     */
//    fun getBarHeightAndMargin(YSteps: Float, barNumber: Int): FloatArray {
//        return calcBarHeightAndMargin(YSteps, barNumber)
//    }

    /**
     * 计算同标签多柱形时的X分隔
     * @param XSteps    X轴步长
     * @param barNumber 柱形个数
     * @return 返回单个柱形的宽度及间距
     */
//    fun getBarWidthAndMargin(XSteps: Float, barNumber: Int): FloatArray {
//        return calcBarWidthAndMargin(XSteps, barNumber)
//    }

    val getBarPaint =
        Paint().apply {
            style = PaintingStyle.Fill
            color = Color.Cyan
        }


    /**
     * 竖向柱形的3D效果
     * @param barLeft    左边X坐标
     * @param barTop    顶部Y坐标
     * @param barRight    右边X坐标
     * @param barBottom    底部Y坐标
     * @param color        柱形颜色
     * @param canvas    画布
     */
    fun renderVertical3DBar(
        barLeft: Float, barTop: Float,
        barRight: Float, barBottom: Float,
        color: Int,
        canvas: Canvas,
    ) {
        //浅色
        //val lightColor: Int = DrawHelper.getInstance().getLightColor(color, mAlpha)

        //getBarPaint().setColor(color)
        //mPaint3D.setColor(lightColor)



        //水平偏移量
        val offsetX = getOffsetX()
        //垂直偏移量
        val offsetY = getOffsetY()


        //Shadow
        val barLeft2 = (barLeft - offsetX).roundToInt().toFloat()
        val barTop2 = (barTop + offsetY).roundToInt().toFloat()
        val barRight2 = (barRight - offsetX).roundToInt().toFloat()
        val barBottom2 = (barBottom + offsetY).roundToInt().toFloat()


        //顶
        mPathRectangle2D.reset()
        mPathRectangle2D.moveTo(barLeft, barTop)
        mPathRectangle2D.lineTo(barLeft2, barTop2)
        mPathRectangle2D.lineTo(barRight2, barTop2)
        mPathRectangle2D.lineTo(barRight, barTop)
        mPathRectangle2D.close()
        canvas.drawPath(mPathRectangle2D, getBarPaint)


        //右侧边
        mPathRectangle2D.reset()
        mPathRectangle2D.moveTo(barRight, barTop)
        mPathRectangle2D.lineTo(barRight2, barTop2)
        mPathRectangle2D.lineTo(barRight2, barBottom2)
        mPathRectangle2D.lineTo(barRight, barBottom)
        mPathRectangle2D.close()
        canvas.drawPath(mPathRectangle2D, getBarPaint)

        mPathRectangle2D.reset()
        mPathRectangle2D.moveTo(barRight2, barTop2)
        mPathRectangle2D.lineTo(barRight2, barBottom2)
        mPathRectangle2D.lineTo(barLeft2, barBottom2)
        mPathRectangle2D.lineTo(barLeft2, barTop2)
        mPathRectangle2D.close()


        //正面 浅色
//        val linearGradient: LinearGradient = LinearGradient(
//            barLeft2, barBottom2, barRight2, barBottom2,
//            intArrayOf(color, lightColor),
//            null, Shader.TileMode.REPEAT
//        )
//        mPaint3D.setShader(linearGradient)
        mPaint3D.style = PaintingStyle.Fill

        canvas.drawPath(mPathRectangle2D, mPaint3D)


        //柱形顶上用白画一个RECT,强化3D效果
        //mPaintLine.reset()
        mPaintLine.style = PaintingStyle.Stroke
        mPaintLine.color=Color.White

        mPathRectangle2D.reset()
        mPathRectangle2D.moveTo(barLeft2, barTop2)
        mPathRectangle2D.lineTo(barRight2, barTop2)
        mPathRectangle2D.lineTo(barRight, barTop)
        canvas.drawPath(mPathRectangle2D, mPaintLine)


        //柱形正面画一根白色竖线,强化3D效果
        canvas.drawLine(Offset( barRight2, barTop2), Offset(barRight2, barBottom2), mPaintLine)
    }

    /**
     * 竖向3D柱形图的底 座
     * @param baseLeft    左边X坐标
     * @param baseTop    顶部Y坐标
     * @param baseRight    右边X坐标
     * @param baseBottom    底部Y坐标
     * @param canvas    画布
     */
//    fun render3DXAxis(
//        baseLeft: Float, baseTop: Float,
//        baseRight: Float, baseBottom: Float,
//        canvas: Canvas,
//    ) //	  int color,
//    {
//        //浅色
//        val baseLightColor: Int = DrawHelper.getInstance().getLightColor(getAxis3DBaseColor(), mAlpha)
//        mPaintBase.setColor(getAxis3DBaseColor())
//        mPaintBase3D.setColor(baseLightColor)
//
//
//        //水平偏移量
//        val offsetX = getOffsetX().toFloat()
//        //垂直偏移量
//        val offsetY = getOffsetY().toFloat()
//
//
//        //Shadow
//        val baseLeft2: Float = MathHelper.getInstance().sub(baseLeft, offsetX)
//        val baseTop2: Float = MathHelper.getInstance().add(baseTop, offsetY)
//        val baseRight2: Float = MathHelper.getInstance().sub(baseRight, offsetX)
//        val baseBottom2: Float = MathHelper.getInstance().add(baseBottom, offsetY)
//
//
//        //顶 用浅色
//        mPathRectangle2D.reset()
//        mPathRectangle2D.moveTo(baseLeft, baseBottom)
//        mPathRectangle2D.lineTo(baseLeft2, baseBottom2)
//        mPathRectangle2D.lineTo(baseRight2, baseBottom2)
//        mPathRectangle2D.lineTo(baseRight, baseBottom)
//        mPathRectangle2D.close()
//        canvas.drawPath(mPathRectangle2D, mPaintBase3D)
//
//
//        //右侧边 深色
//        mPathRectangle2D.reset()
//        mPathRectangle2D.moveTo(baseRight, baseTop)
//        mPathRectangle2D.lineTo(baseRight2, baseTop2)
//        mPathRectangle2D.lineTo(baseRight2, baseBottom2)
//        mPathRectangle2D.lineTo(baseRight, baseBottom)
//        mPathRectangle2D.close()
//        canvas.drawPath(mPathRectangle2D, mPaintBase)
//
//
//        //正面 深色
//        mPathRectangle2D.reset()
//        mPathRectangle2D.moveTo(baseRight2, baseTop2)
//        mPathRectangle2D.lineTo(baseRight2, baseBottom2)
//        mPathRectangle2D.lineTo(baseLeft2, baseBottom2)
//        mPathRectangle2D.lineTo(baseLeft2, baseTop2)
//        mPathRectangle2D.close()
//        canvas.drawPath(mPathRectangle2D, mPaintBase)
//
//
//        //水平偏移量
//        //Paint paint = new Paint();
//        mPaintLine.reset()
//        mPaintLine.setColor(getAxis3DBaseColor())
//        mPaintLine.setStyle(Style.FILL)
//
//        mPathRectangle2D.reset()
//        mPathRectangle2D.moveTo(baseRight2, baseBottom2)
//        mPathRectangle2D.lineTo(baseRight, baseBottom)
//        mPathRectangle2D.lineTo(baseRight, MathHelper.getInstance().add(baseBottom, mAxisBaseThickness))
//        mPathRectangle2D.lineTo(baseRight2, baseBottom2 + mAxisBaseThickness)
//        mPathRectangle2D.close()
//        canvas.drawPath(mPathRectangle2D, mPaintLine)
//        mPaintLine.setColor(baseLightColor)
//        canvas.drawRect(
//            baseLeft2, baseBottom2, baseRight2,
//            baseBottom2 + mAxisBaseThickness, mPaintLine
//        )
//    }


}